package org.concepts.java.antlr.csv;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestCSV {

  // /////////////////// CSV tests/samples ////////////////////////////////////

  /**
   * Validates the most basic case of reading a single record.
   */
  @Test
  public void testOneRecord() {
    
    // Process the CSV input
    CSV csv = new CSV("1,2");

    assertEquals("Row count", 1, csv.getRecordCount());

    // Read the record
    int[] data = csv.getRecord(0);
    assertEquals("Number of integers", 2, data.length);
    assertEquals("First integer", 1, data[0]);
    assertEquals("Second integer", 2, data[1]);
  }

  /**
   * Ensures that multiple records are parsed properly
   */
  @Test
  public void testMultipleRecord() {
    CSV csv = new CSV("1,2\n10, 11, 12\r\n   100, 101, 102");
    assertEquals(3, csv.getRecordCount());

    int[] data = csv.getRecord(0);
    assertEquals(2, data.length);
    assertEquals(1, data[0]);
    assertEquals(2, data[1]);

    data = csv.getRecord(1);
    assertEquals(3, data.length);
    assertEquals(10, data[0]);
    assertEquals(11, data[1]);
    assertEquals(12, data[2]);

    data = csv.getRecord(2);
    assertEquals(3, data.length);
    assertEquals(100, data[0]);
    assertEquals(101, data[1]);
    assertEquals(102, data[2]);

  }

  /**
   * Ensures that comments can be put in the data and are ignored.
   */
  @Test
  public void testWithComments() {
    CSV csv = new CSV("/*Some multi \n line comment */\n"
        + "1,2\n10, 11, 12\r\n //first single line comment\n  100, 101, 102");
    assertEquals(3, csv.getRecordCount());

    int[] data = csv.getRecord(0);
    assertEquals(2, data.length);
    assertEquals(1, data[0]);
    assertEquals(2, data[1]);

    data = csv.getRecord(1);
    assertEquals(3, data.length);
    assertEquals(10, data[0]);
    assertEquals(11, data[1]);
    assertEquals(12, data[2]);

    data = csv.getRecord(2);
    assertEquals(3, data.length);
    assertEquals(100, data[0]);
    assertEquals(101, data[1]);
    assertEquals(102, data[2]);

  }

  /**
   * Ensures that user is informed about errors in the input. Also CSV makes
   * it's best effort to read whatever data it can.
   */
  @Test
  public void testErrorHandling() {
    CSV csv = new CSV(
        "/*Some multi \n line comment */" +
        "// Line with invalid characted \n" +
        "1,b,3\n" +
        "// Line with incorrect separator\n" +
        "10; 11, 12\r\n" +
        "//first single line comment\n" +
        "   100, 101, 102");
    assertEquals(3, csv.getRecordCount());

    int[] data = csv.getRecord(0);
    assertEquals(2, data.length);
    assertEquals(1, data[0]);
    assertEquals(3, data[1]);

    data = csv.getRecord(1);
    assertEquals(2, data.length);
    assertEquals(11, data[0]);
    assertEquals(12, data[1]);

    data = csv.getRecord(2);
    assertEquals(3, data.length);
    assertEquals(100, data[0]);
    assertEquals(101, data[1]);
    assertEquals(102, data[2]);

  }

}
